package com.zxy.allSearcher.datasource;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zxy.allSearcher.common.ErrorCode;
import com.zxy.allSearcher.exception.BusinessException;
import com.zxy.allSearcher.model.entity.Picture;
import com.zxy.allSearcher.model.enums.SearchTypeEnum;
import com.zxy.allSearcher.model.vo.PostVO;
import com.zxy.allSearcher.model.vo.SearchVO;
import com.zxy.allSearcher.model.vo.UserVO;
import com.zxy.allSearcher.utils.PageUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.CompletableFuture;

/**
 * 聚合数据查询服务实现
 *
 * @author zxy
 * @since 2024/6/20
 **/
@Component
public class AllDataSource {
    @Resource
    private DataSourceRegistry dataSourceRegistry;

    /**
     * 有些时候会遇到这种完全无法适配的情况
     * 方法：1、与被适配者服务的提供者沟通，提供适配接口
     */
    public SearchVO doSearch2(String searchText, long current, long size) {
        SearchVO searchVO = new SearchVO();

        // user信息第二
        DataSource<UserVO> userSource = dataSourceRegistry.getDataSourceByType(SearchTypeEnum.USER.getValue());
        Page<UserVO> UserData = userSource.doSearch(searchText, current, size);
        searchVO.setUserList(UserData.getRecords());

        // 默认以文章信息为主，信息一
        DataSource<PostVO> postSource = dataSourceRegistry.getDataSourceByType(SearchTypeEnum.POST.getValue());
        Page<PostVO> PostData = postSource.doSearch(searchText, current, size);
        searchVO.setPostList(PostData.getRecords());

        // picture信息第三
        DataSource<Picture> pictureSource = dataSourceRegistry.getDataSourceByType(SearchTypeEnum.PICTURE.getValue());
        Page<Picture> pictureData = pictureSource.doSearch(searchText, current, size);
        searchVO.setPictureList(pictureData.getRecords());

        return searchVO;
    }

    public SearchVO doSearch(String searchText, long current, long size) {
        SearchVO searchVO = new SearchVO();
        // 任务异步执行
        CompletableFuture<Page<UserVO>> tk1 = CompletableFuture.supplyAsync(() -> {
            // 信息1 用户信息
            DataSource<UserVO> userSource = dataSourceRegistry.getDataSourceByType(SearchTypeEnum.USER.getValue());
            return userSource.doSearch(searchText, current, size);
        });
        CompletableFuture<Page<PostVO>> tk2 = CompletableFuture.supplyAsync(() -> {
            // 信息2 帖子信息
            DataSource<PostVO> postSource = dataSourceRegistry.getDataSourceByType(SearchTypeEnum.POST.getValue());
            return postSource.doSearch(searchText, current, size);
        });
        CompletableFuture<Page<Picture>> tk3 = CompletableFuture.supplyAsync(() -> {
            // 信息3 图片信息
            DataSource<Picture> pictureSource = dataSourceRegistry.getDataSourceByType(SearchTypeEnum.PICTURE.getValue());
            return pictureSource.doSearch(searchText, current, size);
        });

        Page<UserVO> userVOPage;
        Page<PostVO> postVOPage;
        Page<Picture> picturePage;
        try {
            userVOPage = tk1.get();
            postVOPage = tk2.get();
            picturePage = tk3.get();
        } catch (Exception e) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "聚合搜索获取数据有误");
        }
        // 3、封装返回结果
        searchVO.setPostList(postVOPage.getRecords());
        searchVO.setUserList(userVOPage.getRecords());
        searchVO.setPictureList(picturePage.getRecords());
        return searchVO;
    }
}
